﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using Model;
using BLL;

public partial class PersonInfo_MDEdit : BasePage
{
    //private Access access = new Access();
    STOREBO gbo = new STOREBO();
    private static int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {

            if (GetQeuryString("para") == "update")
            {
                string id = GetQeuryString("id");
                STORE gift = gbo.GetModel(int.Parse(id));
                InitData(gift);
            }
            else if (GetQeuryString("para") == "add")
            {
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        STORE gift = GetEntity();
        if (GetQeuryString("para") == "add")
        {

            gbo.Add(gift);
            AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");

        }
        else if (GetQeuryString("para") == "update")
        {
            gbo.Update(gift);
            AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");
        }
    }

    #region 私有方法
    private STORE GetEntity()
    {
        STORE gift = new STORE();
        if (GetQeuryString("para") == "update")
        {
            gift = gbo.GetModel(Convert.ToInt32(GetQeuryString("id")));
        }
        gift.PICURL = txtPicUrl.Text;
        gift.PROMO = txtExplain.Text;
        return gift;
    }
    private void InitData(STORE gift)
    {
        txtName.Text = gift.Name;
        txtNo.Text = gift.CODE;
        txtLXR.Text = gift.CONTACT;
        txtAddress.Text = gift.ADDRESS;
        txtCity.Text = gift.CITY;
        txtExplain.Text = gift.PROMO;
        txtPhone.Text = gift.PHONE;
        txtPicUrl.Text = gift.PICURL;
        txtXZ.Text = gift.ATTR;
        Image1.ImageUrl = gift.PICURL;
        id = gift.ID;
    }

    #endregion
    protected void Button2_Click(object sender, EventArgs e)
    {
        UploadFile();
    }

    private void UploadFile()
    {
        string FileName = file.FileName;
        string path = HttpContext.Current.Request.MapPath("Images/");

        if (!string.IsNullOrEmpty(FileName))
        {
            string extension = System.IO.Path.GetExtension(FileName);//获取文件类型后缀名
            if ((extension.ToLower() == ".jpg"))
            {
                int fileSize = file.PostedFile.ContentLength;
                if (fileSize == 0)
                {
                    base.AlertMessage("文件不存在！");
                }
                else
                {
                    if (true)//限制大小为1M
                    {
                        string guid = Guid.NewGuid().ToString();
                        file.SaveAs(path + guid + ".jpg");  //将文件上传到服务器
                        if (File.Exists(path + guid + ".jpg"))
                        {
                            Image1.ImageUrl = "Images/" + guid + ".jpg";
                            txtPicUrl.Text = ConfigurationManager.AppSettings["ServerURL"] + "/PersonInfo/Images/" + guid + ".jpg";
                            
                        }
                    }
                }
            }
            else
            {
                AlertMessage("请上传JPG格式图片!");
            }
        }
        else
        {
            AlertMessage("请选择文件.");
        }
    }

}